/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#ifndef initializations_H
#define initializations_H

#include <stdio.h>		/* standard i/o definitions */
#include <stdlib.h>		/* standard lib */
#include <math.h>		/* mathematical functions */
#include <string.h>		/* string function definitions */
#include <errno.h>		/* error number definition */
#include <unistd.h>		/* UNIX standard functions */
#include <sys/time.h>		/* time manipulation functions */
#include <signal.h>		/* signal management */

#ifdef SQL_LIBS
//#include <mysql.h>		/* MySQL support */
#include "/usr/include/mysql/mysql.h"
#endif

#include "structures.h"
#include "my_limits.h"
#include "gm4lin.h"

/* logging of data */
#define PROBE_LOG_FILE "log_probe"		/* generic name of the log file */
#define PROBE_LOG_EXTENSION "dat"		/* generic extension of the log file */
#define PROBE_LOG_FILE_PATH "./"		/* path to the data log files */
#ifdef SQL_LIBS
#define SQL_LOG EXIT_FAILURE			/* if we want to log to an sql database */
#define SQL_HOST "gateway"			/* if logging data to a MySQL server, host name */
#define SQL_PORT 0				/* if logging data to a MySQL server, port number. 0 is MySQL default */
#define SQL_DATABASE "lab_data"			/* if logging data to a MySQL server, table name */
#define SQL_TABLE "radiation"			/* if logging data to a MySQL server, table name */
#define SQL_USER "bicron"			/* if logging data to a MySQL server, user name */
#define SQL_PASSWORD "password"			/* if logging data to a MySQL server, password */
#define SQL_QUERY_LENGHT 1023			/* maximum lenght of the sql query string, in chars */
#endif

/* sampling configuration */
#define ACTIVITY_DATA_WRITE_SAMPLING 10		/* sampling rate for data writing in activity recording mode, in seconds */
#define PULSE_DATA_WRITE_SAMPLING 1		/* sampling rate for data writing in pulse mode, in seconds */
#define PULSE_PARTICULE_SUB_SAMPLING 10		/* sampling rate for particules count, in ms */
						/* it has to be an integer fraction of the write_sampling */
/* default values for command line */
#define SERIAL_LOCATION "/dev/tts/"		/* location of the serial ports (generic location),without numbers) */
#define SERIAL_PORTS_START 1			/* we want to watch the serial ports starting on device SERIAL_PORT_START (instead of 0) */
#define SERIAL_PORTS_NUMBER 1			/* default number of serial ports */
#define PULSE_MODE EXIT_FAILURE			/* default recording of activity, not radiation pulse */
#define FILE_LOG EXIT_SUCCESS			/* log data to a file */

extern int syslog_flag;
extern sig_atomic_t flag_alrm;
extern sig_atomic_t flag_stop;

int settings_init(int argn,char *argv[],struct configuration_struct *settings,int sensitivity[]);
int probe_sensitivity_init(struct probe_descriptor *probe,struct configuration_struct *settings,int sensitivity);
int probe_units_init(struct probe_descriptor *probe,struct configuration_struct *settings);
int probe_file_log_init(struct probe_descriptor *probe,struct configuration_struct *settings);
int probes_init(struct probe_descriptor probe[],struct configuration_struct *settings,int sensitivity[]);
#ifdef SQL_LIBS
int sql_init(struct configuration_struct *settings);
#endif
static void sig_alrm(int sig);
static void sig_stop(int sig);
int stop_catch_init(void);
int timer_init(long write_timer_duration, long short_timer_duration);
int timer_sync(void);
int sequencer_init(struct configuration_struct settings);
void daemonize (void);
int change_environement(struct configuration_struct settings);

#endif
